﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using System.ComponentModel;
using EIIS.Infrastructure.Model.System.Authority;
using EIIS.Infrastructure.Service.System.Authority;
using EIIS.Infrastructure.Repositories.System.Authority;
using EIIS.FrameWork.Attributes;

namespace EIIS.Infrastructure.Service.Implement
{
    public class ActionPermissionManager : GenericManagerBase<ActionPermission>, IActionPermissionManager
    {
        public IList<ActionPermission> GetAllActionByAssembly()
        {
            var result = new List<ActionPermission>();

            var types = Assembly.Load("EIIS.Presentation.Controllers").GetTypes();

            foreach (var type in types)
            {
                if (type.BaseType.Name == "BaseController")//如果是Controller
                {
                    var members = type.GetMethods();
                    foreach (var member in members)
                    {
                        if (member.ReturnType.Name == "ActionResult")//如果是Action
                        {
                            var ap = new ActionPermission();

                            ap.ActionName = member.Name;
                            ap.ControllerName = member.DeclaringType.Name.Substring(0, member.DeclaringType.Name.Length - 10); // 去掉“Controller”后缀

                            object[] attrs_Button = member.GetCustomAttributes(typeof(FunctionAttribute), true);
                            if (attrs_Button.LongCount() > 0)
                            {
                                ap.ActionID = (attrs_Button[0] as FunctionAttribute).ButtonID;
                                object[] attrs_Description = member.GetCustomAttributes(typeof(DescriptionAttribute), true);
                                if (attrs_Description.Length > 0)
                                    ap.Description = (attrs_Description[0] as DescriptionAttribute).Description;

                                result.Add(ap);
                            }
                        }

                    }
                }
            }
            return result;
        }

        public IList<ActionPermission> GetAllActionByAssembly(out long total, int page, int rows, string order, string sort,string strQuery)
        {            
            IQueryable<ActionPermission> list;
            if (!string.IsNullOrEmpty(strQuery))
            {
                strQuery = strQuery.ToLower();
                list = GetAllActionByAssembly().Where(a => { return a.ActionName.ToLower().Contains(strQuery) || a.ControllerName.ToLower().Contains(strQuery); }).AsQueryable();
            }
            else
            {
                list = GetAllActionByAssembly().AsQueryable();
            }
            total = list.LongCount();
            if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(sort))
            {
                list = list.OrderBy(sort + " " + order);
            }
            list = list.Skip((page - 1) * rows).Take(rows);

            return list.ToList();
        }


        public IList<ActionPermission> LoadAllWithPage(out long total, int page, int rows, string order, string sort)
        {
            return ((IActionPermissionRepository)(this.CurrentRepository)).LoadAllWithPage(out total, page, rows, order, sort).ToList();
        }

        public IList<ActionPermission> GetAllPageActionByAssembly()
        {
            var result = new List<ActionPermission>();

            var types = Assembly.Load("EIIS.Presentation.Controllers").GetTypes();

            foreach (var type in types)
            {
                if (type.BaseType.Name == "BaseController")//如果是Controller
                {
                    var members = type.GetMethods();
                    foreach (var member in members)
                    {
                        if (member.ReturnType.Name == "ActionResult")//如果是Action
                        {
                            var ap = new ActionPermission();

                            ap.ActionName = member.Name;
                            ap.ControllerName = member.DeclaringType.Name.Substring(0, member.DeclaringType.Name.Length - 10); // 去掉“Controller”后缀

                            object[] attrs_Viewpage = member.GetCustomAttributes(typeof(ViewPageAttribute), true);
                            if (attrs_Viewpage.LongCount() > 0)
                            {
                                if ((attrs_Viewpage[0] as ViewPageAttribute).IsMenu)
                                {
                                    object[] attrs_Description = member.GetCustomAttributes(typeof(DescriptionAttribute), true);
                                    if (attrs_Description.Length > 0)
                                        ap.Description = (attrs_Description[0] as DescriptionAttribute).Description;

                                    result.Add(ap);
                                }
                            }                            
                        }

                    }
                }
            }
            return result;
        }

        public IList<ActionPermission> GetAllPageActionByAssembly(out long total, int page, int rows, string order, string sort, string strQuery)
        {
            IQueryable<ActionPermission> list;
            if (!string.IsNullOrEmpty(strQuery))
            {
                strQuery = strQuery.ToLower();
                list = GetAllPageActionByAssembly().Where(a => { return a.ActionName.ToLower().Contains(strQuery) || a.ControllerName.ToLower().Contains(strQuery); }).AsQueryable();
            }
            else
            {
                list = GetAllPageActionByAssembly().AsQueryable();
            }
            total = list.LongCount();
            if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(sort))
            {
                list = list.OrderBy(sort + " " + order);
            }
            list = list.Skip((page - 1) * rows).Take(rows);

            return list.ToList();
        }
    }
}
